<!DOCTYPE html>
<html>
	<head>
		<title>添加/修改</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../sa-resources/router.js"></script>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<!-- 参数栏 -->
			<div class="c-panel">
				<div class="c-title">添加/修改</div>
				<el-form v-if="m">
					<div class="c-item br">
						<label class="c-label">资源名称：</label>
						<el-input size="mini" v-model="m.resource_name"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">资源编码：</label>
						<el-input size="mini" v-model="m.resource_code"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">所属菜单：</label>
						<el-select
							required="true"
							size="small"
						    v-model="m.menu_id"
						    filterable
						    allow-create
						    default-first-option
						    placeholder="请选择">
						    <el-option v-for="item in parentList" :key="item" :label="item.menu_name" :value="item.menu_id"></el-option>
						</el-select>
					</div>
					<div class="c-item br">
						<label class="c-label">排序：</label>
						<el-input size="mini" v-model="m.sort_num"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">说明：</label>
						<el-input size="mini" v-model="m.intro"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label"></label>
						<el-button size="mini" type="primary" icon="el-icon-plus" size="mini" @click="ok">确定</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<script type="text/javascript">
			new Vue({
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),
					m: {},
					parentList: [],
				},
				methods: {
					// 创建一个 默认Model 
					createModel: function() {
						return {};
					},
					// ok
					ok: function() {
						this.m.created_at = undefined;
						this.m.updated_at = undefined;
						this.m.parentList = undefined;
						sa.ajax(router.updateResource, this.m, function(res){
							sa.alert('操作成功', function(res) {
								sa.closeCurrIframe();	// 关闭本页
								parent.window.location.reload();
							}.bind(this)); 
						}.bind(this));
					}
				},
				mounted: function(){
					// 初始化数据
					// 获取一级菜单
					sa.ajax(router.getMenuList + '?with_tree=0&level=2', function(data){
						this.parentList = data;
					}.bind(this), {type:'get'});
					// 获取数据
					if(this.id <= 0) {
						this.m = this.createModel();
					} else {
						sa.ajax(router.getResource + '?resource_id=' + this.id, function(res) {
							this.m = res;
						}.bind(this), {type: 'get'});
					}
				}
			})
		</script>
	</body>
</html>
